package com.neusoft.service.impl;

import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.mapper.UserMapper;
import com.neusoft.po.User;
import com.neusoft.po.UserGet;
import com.neusoft.service.UserService;
import com.neusoft.util.MD5Lock;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public UserGet getUserByIdByPass(User user) {
		try {
			user.setPassword(MD5Lock.getMD5Str(user.getPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User temp = userMapper.getUserByIdByPass(user);
		if (temp == null) {
			return null;
		}
		UserGet get = new UserGet(temp);
		return get;
	}

	@Override
	public int getUserById(String userId) {
		return userMapper.getUserById(userId);
	}
	
	@Override
	public String getUserNameById(String userId) {
		return userMapper.getUserNameById(userId);
	}

	@Override
	public int saveUser(User user) {
		try {
			user.setPassword(MD5Lock.getMD5Str(user.getPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userMapper.saveUser(user);
	}

	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		try {
			user.setPassword(MD5Lock.getMD5Str(user.getPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userMapper.updateUser(user);
	}

	@Override
	public int removeUser(String userId) {
		// TODO Auto-generated method stub
		return userMapper.removeUser(userId);
	}

}
